/**
 * Created by Administrator on 2018/10/19 0019.
 */

var img = [];
var composeid = location.href.split('/').pop(); //获取url中"/"符后的字串,文章id
composeid = parseInt(composeid);
//获取内容并替换textarea的内容
function getComInfoData(id) {

    layui.use(['laypage','form', 'layer'], function(){
        var html = '';
        var index = layui.layer.load(2, {shade: [0.1,'#fff'],time: 60*1000});
        //验证签名
        sign.params = {
            id: id
        };
        var encode_data = sign.encode_post();
        $.post(url_list.one_compose,encode_data,function(data){
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }
            data = sign.decode_post(data);
            console.log(data);
            layer.close(index);

            info_member(data.data.userid);//左侧个人详情
            post_article(data.data.userid);
            post_time(data.data.userid);

            document.getElementById("title").innerHTML = data.data.title;
            document.getElementById("head_title").innerHTML = data.data.title;
            document.getElementById("title_one").innerHTML = data.data.title;
            document.getElementById("username").innerHTML = data.data.username;
            document.getElementById("time").innerHTML = data.data.createtime;
            document.getElementById("article_img").src = '/'+data.data.compose_img;

            document.getElementById("content").innerHTML = data.data.content;
            // document.getElementById("right_username").innerHTML = data.data.user.username;
            // document.getElementById("introduce").innerHTML = data.data.user.introduce;
            document.getElementById('comments').innerHTML = data.data.count+' Comments';
            document.getElementById('likes').innerHTML = data.data.like;

            if (data.data.count!=0 && data.data.count!=''&& data.data.count!=undefined){
                document.getElementById('count').innerHTML = data.data.count+' Comments';
            }else {
                $('#tihuan_big').html('');
                $('#tihuan_big').removeClass('comment-sec-area pt-80 pb-80');
            }
            if (data.data.img!=null && data.data.img!=''&& data.data.img!=undefined){
                // document.getElementById("right_user_img").src = '/'+data.data.user.img;
                document.getElementById("user_img").src = '/'+data.data.img;
            }
            //评论
            if (data['data']['comment'] === undefined || data['data']['comment'].length == 0) {
                html ='';
                //页面数据替换
                $('#tihuan').html(html);
            }
            else {
                list = data.data.comment;
                for (var i in list) {
                    list[i].id = list[i].id ? list[i].id : '';
                    list[i].content = list[i].content ? list[i].content : '';
                    list[i].createtime = list[i].createtime ? list[i].createtime : '';
                    list[i].img = list[i].img ? '/' + list[i].img : '__static__/img/asset/c4.jpg';
                    list[i].username = list[i].username ? list[i].username : '';
                    list[i].from_userid = list[i].from_userid ? list[i].from_userid : '';

                    html += '<div class="comment-list">'+
                        '<div class="single-comment justify-content-between d-flex">'+
                        '<div class="user justify-content-between d-flex" >';
                    html += '<div class="thumb">';
                    html += '<img style="width: 60px;height: 60px" id="com_img" src="' + list[i].img + '" alt="">'
                    html += '</div>';
                    html += '<div class="desc">';
                    html += '<h5><a href="#" onclick="admin_archive('+list[i].from_userid+')" id="com_username">' + list[i].username + '</a></h5>';
                    html += '<p class="date">' + list[i].createtime + ' </p>';
                    html += '<p class="comment">';
                    html += list[i].content;
                    html += '</p>';
                    html += '</div></div>';

                    html += '<div class="reply-btn">';
                    if (list[i].from_userid == data.data.login_user){
                        html += '<a onclick="cm_del('+list[i].id+')" href="#l" class="btn-reply text-uppercase" > 删除</a>';
                    }
                    html += '<a onclick="reply('+list[i].id+','+composeid+','+list[i].from_userid+')" href="#l" class="btn-reply text-uppercase">reply</a>'+
                        '</div>'+ '</div>';

                    var lists = list[i].reply;
                    //子回复
                    if (lists === undefined || lists.length == 0) {
                    }
                    else {
                        for (var j in lists){
                            lists[j].img= lists[j].img ?'/'+ lists[j].img : '__static__/img/user.jpg';
                            lists[j].username= lists[j].from_user ? lists[j].from_user: '';
                            lists[j].to_user= lists[j].to_user ? lists[j].to_user : '';
                            lists[j].content= lists[j].content ? lists[j].content : '';
                            lists[j].createtime= lists[j].createtime ? lists[j].createtime : '';

                            html += '<div style="margin-left: 30px;margin-top:10px;width: 600px">';
                            html +='<img style="width: 20px;margin-right:5px;height:20px;border-radius: 100%" src="'+lists[j].img+'" alt="">';
                            html +='<a href="#" onclick="admin_archive('+lists[j].from_userid+')">'+lists[j].from_user+'</a> 回复 '+lists[j].to_user+'：';
                            html +='<em style="width: 500px">'+lists[j].content+'</em>';
                            html +='<em class="date" style="margin-left: 20px"> '+lists[j].createtime+' </em>';
                            html +='<a onclick="reply('+list[i].id+','+composeid+','+lists[j].from_userid+')" href="#l" style="margin-left: 20px;color: #00b3ee" > 回复</a>';

                            if (lists[j].from_userid == data.data.login_user){
                                html +='<a onclick="reply_del('+lists[j].id+')" href="#l" style="margin-left: 20px;color: #00b3ee" > 删除</a>';
                            }
                            html +='</div>';
                        }
                    }
                    html +='</div>';
                }
                $('#tihuan').html(html);
            }
        })
    });
}
//评论
function cm_add() {
    var content =$('#comment').val();
    var id = location.href.split('/').pop();
    id =parseInt(id);
    sign.params={
        compose_id : id,
        content:content
    };
    var encode_data = sign.encode_post();
    $.post(url_list.cm_add,encode_data,function (data) {
        //解码
        if(typeof data == 'string'){
            data = eval('('+data+')');
        }
        data = sign.decode_post(data);

        if (data.code == 200) {
            layer.msg(data.msg, {icon: 1, time: 1500});
            $('#comment').val('');
            getComInfoData(composeid);
        } else {
            layer.msg(data.msg, {icon: 2});
        }
    })
}
//点赞
function like_add(type,userid) {
    if(type==1){
        sign.params={
            type_id:composeid,
            type:type
        };
    }else {
        sign.params={
            type_id:userid,
            type:type
        };
    }

    var encode_data = sign.encode_post();
    $.post(url_list.like_add,encode_data,function (data) {
        //解码
        if(typeof data == 'string'){
            data = eval('('+data+')');
        }
        data = sign.decode_post(data);
        console.log(data);
        if (data.code == 200) {
            layer.msg(data.msg, {icon: 1, time: 1500});
            $('#comment').val('');
            getComInfoData(composeid);
        } else {
            layer.msg(data.msg, {icon: 2});
        }
    })
}
//回复
function reply(comment_id,reply_id,to_userid){
    //弹窗
    layer.prompt({
        formType: 2,
        // value:'请输入内容',
        title: '回复',
        area: ['400px', '50px'] //自定义文本域宽高
    }, function(value, index, reply_content){
        sign.params={
            comment_id:comment_id,
            reply_id:reply_id,
            reply_type:1,
            to_userid:to_userid,
            content:value
        };
        var encode_data = sign.encode_post();
        $.post(url_list.reply_add,encode_data,function (data) {
            //解码
            if(typeof data == 'string'){
                data = eval('('+data+')');
            }
            data = sign.decode_post(data);
            console.log(data);
            if (data.code == 200) {
                layer.msg(data.msg, {icon: 1, time: 1500});
                getComInfoData(composeid)
            } else {
                layer.msg(data.msg, {icon: 2});
            }
        })
        layer.close(index);
    });
}

function reply_del(id) {
    confirm("确定要删除吗？");
    sign.params={
        id:id,
        del:0
    };
    var encode_data = sign.encode_post();
    $.post(url_list.reply_del,encode_data,function (data) {
        //解码
        if(typeof data == 'string'){
            data = eval('('+data+')');
        }
        data = sign.decode_post(data);
        console.log(data);
        if (data.code == 200) {
            layer.msg(data.msg, {icon: 1, time: 1500});
            getComInfoData(composeid);
        } else {
            layer.msg(data.msg, {icon: 2});
        }
    })
}

function cm_del(id) {
    if (confirm("确定要删除？")) {

        sign.params={
            id:id,
            del:0
        };
        var encode_data = sign.encode_post();
        $.post(url_list.cm_del,encode_data,function (data) {
            //解码
            if(typeof data == 'string'){
                data = eval('('+data+')');
            }
            data = sign.decode_post(data);
            console.log(data);
            if (data.code == 200) {
                layer.msg(data.msg, {icon: 1, time: 1500});
                getComInfoData(composeid);
            } else {
                layer.msg(data.msg, {icon: 2});
            }
        })
    }
}